package io.jt808.proto.cmd;

import io.norland.buffer.AutoBuffer;
import io.norland.annotations.Proto;
import io.norland.proto.IProto;
import lombok.Data;
import lombok.Getter;
import lombok.Setter;

import java.util.ArrayList;
import java.util.List;

/**
 * 提问下发
 * 0  1：紧急
 * 3  1：终端 TTS 播读
 * 4  1：广告屏显示
 */
@Proto("8302")
@Data
public class JT8302 implements IProto {
    /**
     * 标志
     */
    private byte flag;
    /**
     * 问题内容长度
     */
    private short questionLength;
    /**
     * 问题
     */
    private String question;
    /**
     * 候选答案列表
     */
    private List<AnswerItem> candidateAnswers;

    public byte[] writeToBytes() {
        AutoBuffer buff = new AutoBuffer();
        buff.writeByte(flag);
        buff.writeByte(questionLength);
        buff.writeGbkString(question, questionLength);
        for (AnswerItem ai : candidateAnswers) {
            buff.writeByte(ai.answerId);
            buff.writeShort(ai.answerLength);
            buff.writeGbkString(ai.answerContent, ai.answerLength);
        }
        return buff.array();

    }

    public void readFromBuf(AutoBuffer buff) {
        flag = buff.readByte();
        questionLength = buff.readByte();
        question = buff.readGbkString(questionLength);
        candidateAnswers = new ArrayList<>();
        while (buff.isReadable()) {
            AnswerItem ai = new AnswerItem();
            ai.answerId = buff.readByte();
            ai.answerLength = buff.readShort();
            ai.answerContent = buff.readGbkString(ai.answerLength);
            candidateAnswers.add(ai);
        }

    }

    @Getter
    @Setter
    public class AnswerItem {
        /**
         * 答案ID
         */
        private byte answerId;
        /**
         * 答案内容长度
         */
        private short answerLength;
        /**
         * 答案内容
         */
        private String answerContent;
    }

//    public static void main(String[] args) {
//        byte[] bytes = new byte[]{
//                0x00,
//                0x05,
//                (byte) 0xBA, (byte) 0xBD, (byte) 0xD4, (byte) 0xBA,
//                0x38,
//                0x01,
//                0x00, 0x05,
//                (byte) 0xBA, (byte) 0xBD, (byte) 0xD4, (byte) 0xBA,
//                0x38
//        };
//        JT8302 jt8301 = new JT8302();
//        jt8301.readFromBuf(new AutoBuffer(bytes));
//        System.out.println(new String(bytes).equals(new String(jt8301.writeToBytes())));
//    }
}